<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NButton,
  NCheckbox,
  NCheckboxGroup,
  NForm,
  NFormItem,
  NImage,
  NInput,
  NSpace,
  NSwitch,
  NTag,
  NPopconfirm,
  NLayout,
  NLayoutSider,
  NLayoutContent,
  NTreeSelect,
  useMessage,
  NFlex,
  NAvatar,
  NSelect, NUpload,
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import { getToken } from '@/utils'
import { formatDate, renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
import api from '@/api'
import TheIcon from '@/components/icon/TheIcon.vue'
import { useUserStore } from '@/store'

const model_name = "耗材消耗合计"
defineOptions({ name: model_name })
const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')
const base_path = import.meta.env.VITE_BASE


const {
  modalVisible,
  modalTitle,
  modalAction,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: model_name,
  initForm: {
    interval:30
  },
  doCreate: api.createConsumables,
  doUpdate: api.updateConsumables,
  doDelete: api.deleteConsumables,
  refresh: () => $table.value?.handlerefresh(),
})
const data = reactive({

})

const timestamp = ref(getCurrentMonthTimestamp());
function getCurrentMonthTimestamp() {
  const now = new Date();
  const year = now.getFullYear();
  const month = now.getMonth();
  const firstDayOfMonth = new Date(year, month, 1);
  return firstDayOfMonth.getTime();
}


function formatTimestamp(timestamp) {
  const date = new Date(timestamp)
  const pad = (num) => num.toString().padStart(2, '0')
  const year = date.getFullYear()
  const month = pad(date.getMonth() + 1) // 月份从0开始，所以需要+1
  const day = pad(date.getDate())
  const hours = pad(date.getHours())
  const minutes = pad(date.getMinutes())
  const seconds = pad(date.getSeconds())
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const handleDateRangeChange = (value) => {
  queryItems.value.start_time = formatTimestamp(value)
}

onMounted(() => {
  queryItems.value.tongji = 1
  $table.value?.handleSearch()
})

const columns = [
  {
    title: 'ID',
    key: 'id',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '耗材名称',
    key: 'name',
    width: 60,
    align: 'center',
    ellipsis: { tooltip: true },
    fixed: 'left',
  },

  {
    title: '类别',
    key: 'type_name',
    align: 'center',
    width: 60,
    ellipsis: { tooltip: true },
    render(row) {
      return h(
        NTag,
        { type: 'primary', size: 'small', bordered: false, style: { margin: '2px 3px' } },
        { default: () => row.type_name }
      )
    },
  },
    {
    title: '规格',
    key: 'guige',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },
    {
    title: '单位',
    key: 'danwei',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },

    {
    title: '排序',
    key: 'order',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },
     {
    title: '消耗合计',
    key: 'used',
    width: 30,
    align: 'center',
    ellipsis: { tooltip: true },
  },

]


</script>

<template>

      <CommonPage show-footer :title="model_name">
        <!-- 表格 -->
        <CrudTable
          ref="$table"
          v-model:query-items="queryItems"
          :columns="columns"
          :get-data="api.getConsumables"
          :scroll-x="1200"
        >
          <template #queryBar>
            <QueryBarItem label="耗材名称" :label-width="80">
              <NInput
                v-model:value="queryItems.name"
                clearable
                type="text"
                placeholder="请输入耗材名称"
                @keypress.enter="$table?.handleSearch()"
              />
            </QueryBarItem>
            <QueryBarItem label="统计月份" :label-width="80">
              <n-date-picker
                v-model:value="timestamp"
                type="month"
                format="y年 M月"
                year-format="y年"
                month-format="M月"
                clearable
                @update:value="handleDateRangeChange"
              />
            </QueryBarItem>
          </template>
        </CrudTable>
      </CommonPage>

  <!-- 业务页面 -->
</template>
